package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.*;
import com.tjetc.service.*;
import com.tjetc.service.impl.*;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/fee/*")
public class FeeServlet extends HttpServlet {
    private FeeService feeService = new FeeServiceImpl();
    private OtcService otcService = new OtcServiceImpl();
    private MedicineService medicineService = new MedicineServiceImpl();
    private RecordService recordService = new RecordServiceImpl();
    private DoctorService doctorService = new DoctorServiceImpl();
    private BedService bedService = new BedServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //无需住院的用户，添加药品费用到fee表
    protected void addMed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer rid = Integer.valueOf(request.getParameter("rid"));
        Integer oid = Integer.valueOf(request.getParameter("oid"));
        List<Otc> otcList = otcService.listByRid(rid);
        double account = 0.0;
        for (Otc otc : otcList) {
            Medicine medicine = medicineService.getById(otc.getMid());
            account += (medicine.getPrice()*otc.getNum());
        }
        Fee fee = new Fee();
        fee.setRid(rid);
        fee.setType("药品费用");
        fee.setAccount(account);
        boolean b = feeService.add(fee,oid);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("就诊成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("就诊失败，出异常喽！"));
    }

    //需住院的用户，添加病床费用到fee
    protected void addBed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer rid = Integer.valueOf(request.getParameter("rid"));
        Integer did = Integer.valueOf(request.getParameter("did"));
        Integer oid = Integer.valueOf(request.getParameter("oid"));
        Doctor doctor = doctorService.getById(did);
        List<Bed> beds = bedService.findByDepid(doctor.getDepid());
        double bedFee = 0.0;
        for (Bed bed : beds) {
          bedFee = bed.getPrice();
          break;
        }
        Fee fee = new Fee();
        fee.setRid(rid);
        fee.setType("病床费用");
        fee.setAccount(bedFee);
        boolean b = feeService.add(fee,oid);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("就诊成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("就诊失败，出异常喽！"));
    }

    //患者查询我的缴费信息
    protected void listMyFee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        List<Record> records = recordService.listMyRecord(uid);
        List<Fee> fees = new ArrayList<>();
        for (Record record : records) {
            Fee fee = feeService.listMyFee(record.getId());
            fees.add(fee);
        }
        new ObjectMapper().writeValue(response.getOutputStream(),fees);
    }

    //根据缴费信息查询统计报表
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date=request.getParameter("value");
        List<Medreport> list=feeService.listByDate(date);
        new ObjectMapper().writeValue(response.getOutputStream(),list);
    }

    //查询所有缴费信息
    protected void listAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = feeService.getCount();
        PageInfo<Fee> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Fee> list = feeService.list(pageInfo);
        pageInfo.setList(list);
        System.out.println("缴费信息 = " + list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    protected void FinalFee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Integer rid = Integer.valueOf(request.getParameter("rid"));
        boolean b = feeService.finishFee(id,rid);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("处理完成"));
        }else{
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("处理失败，相关药品库存不足"));
        }
    }

    protected void listNoDeal(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Fee> list = feeService.listNoDeal();
        new ObjectMapper().writeValue(response.getOutputStream(), list);
    }

}
